//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    #region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ComponentModel;
    using System.Workflow.ComponentModel;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Workflow.ComponentModel.Design;
    using System.ComponentModel.Design;
    using Microsoft.Research.ScientificWorkflow.NetCDF.CSharpAPI;
    using System.Workflow.ComponentModel.Compiler;

    using Microsoft.Research.ScientificWorkflow;
    using Microsoft.Research.ScientificWorkflow.Helpers;
    
    using WorkflowData;
    #endregion

    [Name("Variable Renamer Transform")]
    [Description("Transform for renaming a variable")]
    [WorkflowCategory("NetCdf")]
    public partial class VariableRenamerActivity: Activity
    {
        public VariableRenamerActivity()
		{
			InitializeComponent();
		}

		#region Dependenty Proproperty

		public static DependencyProperty OldVariableNameProperty = DependencyProperty.Register("OldVariableName", 
            typeof(string), typeof(VariableRenamerActivity));

        [RequiredInputParam]
        [Name("Original Name", "Original Variable Name")]
        [Description("Name of the variable - that will be renamed. All other variables will be populated as it is.")]
		public string OldVariableName
		{
			get
			{
				return ((string)(base.GetValue(VariableRenamerActivity.OldVariableNameProperty)));
			}
			set
			{
				base.SetValue(VariableRenamerActivity.OldVariableNameProperty, value.Trim());
			}
		}

		public static DependencyProperty NewVariableNameProperty = DependencyProperty.Register("NewVariableName", 
            typeof(string), typeof(VariableRenamerActivity));

        [RequiredInputParam]
        [Name("New Name", "New Variable Name")]
        [Description("New name for the variable")]
		public string NewVariableName
		{
			get
			{
				return ((string)(base.GetValue(VariableRenamerActivity.NewVariableNameProperty)));
			}
			set
			{
				#region Check for valid variable name > no whitespaces , no special char < Refer netCDF(nc) documentations
				value = value.Trim();
				if (value.Contains(" "))
					throw new ArgumentException("Variable name cannot contain white spaces.");
				for (int i = 0; i < value.Length; i++)
				{
					if (i == 0 && (!char.IsLetter(value[i]) && value[i] != '_'))
					{
						throw new ArgumentException("Variable name should start with a letter or underscore.");
					}
					if (!Char.IsLetterOrDigit(value[i]) && value[i] != '_' && value[i] != '.')
					{
						throw new ArgumentException("Not a valid variable name. Must be formed of letter, digits, underscore or period.");
					}
				}
				#endregion

				base.SetValue(VariableRenamerActivity.NewVariableNameProperty, value);
			}
		}

        public static DependencyProperty InputCubeProperty =
            DependencyProperty.Register("InputCube", typeof(HyperCube), typeof(VariableRenamerActivity));

        [RequiredInputParam]
        [Name("In Hyper Cube")]
        [Description("Hyper Cube for Cleaning")]
        public HyperCube InputCube
        {
            get
            {
                return ((HyperCube)(base.GetValue(VariableRenamerActivity.InputCubeProperty)));
            }
            set
            {
                base.SetValue(VariableRenamerActivity.InputCubeProperty, value);
            }
        }

        public static DependencyProperty OutCubeProperty =
            DependencyProperty.Register("OutCube", typeof(HyperCube), typeof(VariableRenamerActivity));

        [OutputParam]
        [Name("Resultant Hyper Cube")]
        [Description("Cleaned cube containing data with cleaned values.")]
        public HyperCube OutCube
        {
            get
            {
                return ((HyperCube)(base.GetValue(VariableRenamerActivity.OutCubeProperty)));
            }
            set
            {
                base.SetValue(VariableRenamerActivity.OutCubeProperty, value);
            }
        }

		#endregion

		#region Overriden methods
		protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
		{
			HyperCubeSchema schema = this.InputCube.Schema;

			// Update the name in the hypercube
            this.InputCube.Schema.SetName(this.InputCube.Schema.LookupColumnIndex(this.OldVariableName), 
                this.NewVariableName);

            this.OutCube = this.InputCube;
			return ActivityExecutionStatus.Closed;
		}
		#endregion


	}


	public partial class VariableRenamerActivity
	{
		#region Designer generated code

		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		[System.Diagnostics.DebuggerNonUserCode]
		private void InitializeComponent()
		{
			this.Name = "VariableRenamerActivity";
		}

		#endregion
	}

}
